package com.orange.common.popup

import android.content.Context
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.setViewTreeLifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.lxj.xpopup.util.XPopupUtils

/**
 * Created by Orange on 2022/11/13 12:34.
 */
abstract class BasePopup<DB : ViewDataBinding> : CenterPopupView {

    protected lateinit var binding: DB

    private lateinit var mLifecycle: Lifecycle

    private lateinit var popupManage: PopupManage

    private val builder: XPopup.Builder by lazy { XPopup.Builder(null) }

    /**
     * 与 activity 生命周期绑定
     */
    constructor(context: Context = ActivityUtils.getTopActivity()) : super(context) {
        if (context is FragmentActivity) {
            mLifecycle = context.lifecycle
        }
        initBuilder()
    }

    private fun initBuilder() {
        builder.apply {
            dismissOnBackPressed(false) // 按返回键是否关闭弹窗
            dismissOnTouchOutside(false) // 点击外部是否关闭弹窗
            maxWidth(XPopupUtils.getAppWidth(context))//设置弹窗的最大宽度
            //customHostLifecycle(mLifecycle)// 监视lifecycle的生命周期,自动销毁
        }.asCustom(this)

        popupManage = PopupManage.getInstance(mLifecycle)
    }

    override fun onCreate() {
        super.onCreate()
        binding = DataBindingUtil.bind(popupImplView)!!
        binding.lifecycleOwner = this
        binding.root.setViewTreeLifecycleOwner(this)
        initView()
    }

    override fun getImplLayoutId() = getLayoutId()

    protected abstract fun getLayoutId(): Int

    protected abstract fun initView()

    /**
     * builder 常用设置 https://github.com/li-xiaojun/XPopup/wiki/5.-%E5%B8%B8%E7%94%A8%E8%AE%BE%E7%BD%AE
     */
    fun build(block: XPopup.Builder.() -> Unit): BasePopup<DB> {
        builder.apply {
            block()
        }.asCustom(this)
        return this
    }

    fun doOnDismiss(onDismiss: (popupView: BasePopupView?) -> Unit = {}): BasePopup<DB> {
        return addListener(onDismiss = onDismiss)
    }

    fun addListener(
        onCreate: (popupView: BasePopupView?) -> Unit = {},
        beforeShow: (popupView: BasePopupView?) -> Unit = {},
        onShow: (popupView: BasePopupView?) -> Unit = {},
        onDismiss: (popupView: BasePopupView?) -> Unit = {},
        beforeDismiss: (popupView: BasePopupView?) -> Unit = {},
        onBackPressed: (popupView: BasePopupView?) -> Boolean = { false },
    ): BasePopup<DB> {
        return build {
            setPopupCallback(object : SimpleCallback() {
                override fun onCreated(popupView: BasePopupView?) {
                    onCreate.invoke(popupView)
                }

                override fun beforeShow(popupView: BasePopupView?) {
                    beforeShow.invoke(popupView)
                }

                override fun onShow(popupView: BasePopupView?) {
                    onShow.invoke(popupView)
                }

                override fun onDismiss(popupView: BasePopupView?) {
                    onDismiss.invoke(popupView)
                }

                override fun beforeDismiss(popupView: BasePopupView?) {
                    beforeDismiss.invoke(popupView)
                }

                override fun onBackPressed(popupView: BasePopupView?): Boolean {
                    return onBackPressed.invoke(popupView)
                }
            })
        }
    }

    /**
     * 可实现连续弹窗
     */
    fun addShow() {
        popupManage.addShow(this)
    }

    override fun dismiss() {
        if (!isShow) {
            // 如果还没有显示就从队列中移除
            popupManage.remove(this)
        }
        super.dismiss()
    }

    /**
     * 弹窗关闭回调 然后自动显示下一个弹窗
     */
    override fun onDismiss() {
        super.onDismiss()
        popupManage.showNext(this)
    }

}